package net.qiqbframework.messaging;

import javax.annotation.Nonnull;
import java.util.Map;

public abstract class AbstractMessage<T> implements Message<T> {


    private final String identifier;

    public AbstractMessage(String identifier) {
        this.identifier = identifier;
    }

    @Override
    public String getIdentifier() {
        return identifier;
    }

    @Override
    public void withMetaData(@Nonnull Map<String, ?> metaData) {
        if (getMetaData().equals(metaData)) {
            return;
        }
        withMetaData(MetaData.of(metaData));
    }

    @Override
    public void andMetaData(@Nonnull Map<String, ?> metaData) {
        if (metaData.isEmpty()) {
            return;
        }
        withMetaData(getMetaData().mergedWith(metaData));
    }

    /**
     * Returns a new message instance with the same payload and properties as this message but given {@code metaData}.
     *
     * @param metaData The metadata in the new message
     * @return a copy of this instance with given metadata
     */
    protected abstract void withMetaData(MetaData metaData);


}
